package com.cn.ant.modules.score.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.modules.score.dao.BatchLineDao;
import com.cn.ant.modules.score.entity.BatchLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批次线Service
 *
 * @author AntDream
 * @version 2015-08-18
 */
@Service
@Transactional(readOnly = true)
public class BatchLineService extends BaseService {

	@Autowired
	private BatchLineDao batchLineDao;

	public BatchLine get(String id) {
		return batchLineDao.get(id);
	}

	public List<BatchLine> findList(BatchLine batchLine) {
		return batchLineDao.findList(batchLine);
	}

	public Page<BatchLine> findPage(Page<BatchLine> page, BatchLine batchLine) {
		batchLine.setPage(page);
		page.setList(batchLineDao.findList(batchLine));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(BatchLine batchLine) {
		if (batchLine.getIsNewRecord()) {
			batchLine.preInsert();
			batchLineDao.insert(batchLine);
		} else {
			batchLine.preUpdate();
			batchLineDao.update(batchLine);
		}
	}

	@Transactional(readOnly = false)
	public void batchInsert(List<BatchLine> batchLineList) {
		if (batchLineList != null && !batchLineList.isEmpty()) {
			for (int i = 0; i < batchLineList.size(); i++) {
				batchLineList.get(i).preInsert();
				batchLineList.get(i).setId(Identities.generateUUID());
			}
			batchLineDao.batchInsert(batchLineList);
		}
	}

	@Transactional(readOnly = false)
	public void delete(BatchLine batchLine) {
		batchLineDao.delete(batchLine);
	}

	/**
	 * 查询录取线
	 * @param year
	 * @return
	 */
	public List<Map<String, Object>> queryByYear(int year) {
		return batchLineDao.queryByYear(year);
	}

	/**
	 * 获取批次线分数
	 * @param year
	 * @param province
	 * @param kemuType
	 * @param batchLine
	 * @return
	 */
	public int getBatchLineScore(int year, String province, String kemuType, String batchLine) {
		//查询指定年份的批次录取线
		List<Map<String, Object>> batchLineMap = batchLineDao.queryByYear(year);
		if (batchLineMap != null && batchLineMap.size() > 0) {
			for (int i = 0; i < batchLineMap.size(); i++) {
				Map<String, Object> temp = batchLineMap.get(i);
				if (StringUtils.equals(province, (String)temp.get("provice")) && (Integer)temp.get("year") == year
						&& StringUtils.equals(kemuType, (String)temp.get("type")) && StringUtils.equals(batchLine, (String)temp.get("batch_level"))) {
					Double batchScore = (Double)temp.get("batch_score");
					return batchScore.intValue();
				}
			}
		}
		return 0;
	}

	public List<Map<String, String>> batchList(String province, String type) {
		Map<String, String> params = new HashMap<>();
		params.put("province", province);
		params.put("type", type);
		return batchLineDao.batchList(params);
	}

}